//
//  BaseCollectionView.m
//  ArtDog
//
//  Created by dllo on 16/8/31.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "BaseCollectionView.h"

@interface BaseCollectionView ()

@property (nonatomic, copy) NumbersOfSection numberOfSection;
@property (nonatomic, copy) NumberOfItems numberOfItems;
@property (nonatomic, copy) CellForItemAtIndexPath cellForItemAtIndexPath;

@property (nonatomic, copy) DidSelectItemAtIndexPath didSelectItemAtIndexPath;
@property (nonatomic, strong) CollectionViewReturnParam *collectionViewParam;

@end

@implementation BaseCollectionView

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout
{
    self = [super initWithFrame:frame collectionViewLayout:layout];
    if (self) {
        [self setupBasic];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setupBasic];
    }
    return self;
}

- (void)setupBasic
{
    self.backgroundColor = [UIColor whiteColor];
}

- (void)collectionViewWithNumberOfSection:(NumbersOfSection)numberOfSection andNumberOfItems:(NumberOfItems)numberOfItems andCellForitemAtIndexPath:(CellForItemAtIndexPath)cellForItemAtIndexPath andDidSelectItemAtIndexPath:(DidSelectItemAtIndexPath)didSelectItemAtIndexPath
{
    self.delegate = self;
    self.dataSource = self;
    _numberOfItems = numberOfItems;
    _numberOfSection = numberOfSection;
    _cellForItemAtIndexPath = cellForItemAtIndexPath;
    _didSelectItemAtIndexPath = didSelectItemAtIndexPath;
    _collectionViewParam = [[CollectionViewReturnParam alloc]init];
}



- (void)collectionViewWithNumberOfItems:(NumberOfItems)numberOfItems andCellForItemAtIndexPath:(CellForItemAtIndexPath)cellForItemAtIndexPath andDidSelectItemAtIndexPath:(DidSelectItemAtIndexPath)didSelectItemAtIndexPath
{
    [self collectionViewWithNumberOfSection:^(UICollectionView * _Nonnull collectionView, CollectionViewReturnParam * _Nonnull collectionViewParam) {
        
    } andNumberOfItems:numberOfItems andCellForitemAtIndexPath:cellForItemAtIndexPath andDidSelectItemAtIndexPath:didSelectItemAtIndexPath];
}

- (void)collectionViewWithNumberOfItems:(NumberOfItems)numberOfItems andCellForItemAtIndexPath:(CellForItemAtIndexPath)cellForItemAtIndexPath
{
    [self collectionViewWithNumberOfSection:^(UICollectionView * _Nonnull collectionView, CollectionViewReturnParam * _Nonnull collectionViewParam) {
        
    } andNumberOfItems:numberOfItems andCellForitemAtIndexPath:cellForItemAtIndexPath andDidSelectItemAtIndexPath:^(UICollectionView * _Nonnull collectionView, NSIndexPath * _Nonnull indexPath) {
        
    }];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    NSInteger numberSection = 1;
    self.numberOfSection(collectionView, _collectionViewParam);
    if (_collectionViewParam.numberOfSection > 0) {
        numberSection = _collectionViewParam.numberOfSection;
    }
    return numberSection;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSInteger numberOfItems = 1;
    self.numberOfItems(collectionView, _collectionViewParam);
    numberOfItems = _collectionViewParam.numberOfItems;
    return numberOfItems;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell;
    self.cellForItemAtIndexPath(collectionView, indexPath, _collectionViewParam);
    cell = _collectionViewParam.returnCell;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.didSelectItemAtIndexPath(collectionView, indexPath);
}

@end

@implementation CollectionViewReturnParam

- (instancetype)init
{
    self = [super init];
    if (self) {
        _numberOfSection = 1;
    }
    return self;
}

@end

